WHEN ERRor

Syntax: WHEN ERRor
Location: QL ROM (post JM), THOR XVI

This command marks the beginning of the SuperBASIC structure which is used to surround lines of SuperBASIC code which should be executed whenever an error is generated whilst error trapping is active. Error trapping is activated as soon as the interpreter reads a line containing WHEN ERRor. It is therefore not activated by a WHEN ERRor command being entered into the command window (#0) - indeed this has a special purpose (see below).

The syntax of the SuperBASIC structure can take two forms:

WHEN ERROR: statement {:statement}
or
WHEN ERROR
{statement}
..
END WHEN

Once error trapping is enabled, whenever an error is generated, control is passed to the WHEN ERRor clause, allowing you to specify how the error is to be dealt with. 

If the interpreter comes across more than one WHEN ERRor block, then the latest one is used to trap errors. Errors generated within the WHEN ERRor block itself are reported as normal.

Unless you include a STOP statement in the WHEN ERRor clause, after going through all of the lines within the clause, the program will continue running from the statement following the one which caused the error. You can force this to happen with CONTINUE, whereas RETRY can be used to re-execute the command which caused the error.

Error trapping is turned off by the command WHEN ERRor (when entered as a direct command), and also when the NEW, CLEAR, LOAD, LRUN, MERGE, and MRUN commands are issued.

Example:
100 WHEN ERRor
110   STRIP#0,2
160   IF ERR_OV
170     PRINT#0,'Divide by zero is undefined!'\'Press a key'
180     PAUSE:STRIP #0,0:RETRY 320
190   END IF
220 END WHEN

CROSS-REFERENCE:
ERLIN returns the line number on which the error occurred. 
ERNUM returns the error number itself. 
BREAK_OFF allows you to turn the Break key off. 
END WHEN defines the end of the error handling block.
